﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using PassBag;
using PassBag.Utils;
using PassBag.Logic;

namespace TestPassBag {
    [TestFixture, Description ("Test the ListSynchronizer class")]
    public class TestListSynchronizer {

        [Test, Description ("Test ListSynchronizer class with sorted equal length list")]
        public void TestSynchronizeEqualLengthList () {

            List<string> sourceList = new List<string> { "Black", "Blue", "Green" };
            List<string> destinationList = new List<string> { "Black", "Blue", "Yellow" };

            List<string> s = new List<string> ();
            List<string> d = new List<string> ();


            ListSynchronizer.SynchronizeSortedList (sourceList, destinationList, StringComparer.CurrentCultureIgnoreCase,
                                                     s, d);

            Assert.IsTrue (s[0].Equals ("Green"));
            Assert.IsTrue (d[0].Equals ("Yellow"));

            Assert.IsTrue (s.Count == 1);
            Assert.IsTrue (d.Count == 1);
        }

        [Test, Description ("Test ListSynchronizer class with sorted not equal length list")]
        public void TestSynchronizeDifferenteLengthList () {

            List<string> sourceList = new List<string> { "Black", "Blue", "Cyan", "Green" };
            List<string> destinationList = new List<string> { "Black", "Blue", "Yellow" };

            List<string> s = new List<string> ();
            List<string> d = new List<string> ();

            ListSynchronizer.SynchronizeSortedList (sourceList, destinationList, StringComparer.CurrentCultureIgnoreCase,
                s, d);

            Assert.IsTrue (s[0].Equals ("Cyan"));
            Assert.IsTrue (s[1].Equals ("Green"));
            Assert.IsTrue (d[0].Equals ("Yellow"));


            Assert.IsTrue (s.Count == 2);
            Assert.IsTrue (d.Count == 1);
        }

        [Test, Description ("Test ListSynchronizer class with PasswordObject list")]
        public void TestSynchronizeByIdPasswordObjectList () {

            List<PasswordObject> sourcePasswordObjectList = new List<PasswordObject> ();
            List<PasswordObject> destinationPasswordObjectList = new List<PasswordObject> ();

            Guid guid1 = new Guid ("{CA761232-ED42-11CE-BACD-00AA0057B223}");
            Guid guid2 = new Guid ("{CA761232-ED42-11CE-BACD-00AA0057B224}");
            Guid guid3 = new Guid ("{DA761232-ED42-11CE-BACD-00AA0057B223}");
            Guid guid4 = new Guid ("{EA761232-ED42-11CE-BACD-00AA0057B223}");
            Guid inSourceOnly1 = Guid.NewGuid ();
            Guid inSourceOnly2 = Guid.NewGuid ();
            Guid inDestinationOnly1 = Guid.NewGuid ();

            sourcePasswordObjectList.Add (new PasswordObject (guid3));
            sourcePasswordObjectList.Add (new PasswordObject (guid2));
            sourcePasswordObjectList.Add (new PasswordObject (guid4));
            sourcePasswordObjectList.Add (new PasswordObject (guid1));
            sourcePasswordObjectList.Add (new PasswordObject (inSourceOnly1));
            sourcePasswordObjectList.Add (new PasswordObject (inSourceOnly2));

            destinationPasswordObjectList.Add (new PasswordObject (guid2));
            destinationPasswordObjectList.Add (new PasswordObject (guid3));
            destinationPasswordObjectList.Add (new PasswordObject (guid1));
            destinationPasswordObjectList.Add (new PasswordObject (guid4));
            destinationPasswordObjectList.Add (new PasswordObject (inDestinationOnly1));

            PasswordObjectComparer comp = new PasswordObjectComparer ();
            comp.ComparisonMethod = PasswordObjectComparer.ComparisonType.Id;

            // sort the source and destination list
            sourcePasswordObjectList.Sort (comp);
            destinationPasswordObjectList.Sort (comp);

            List<PasswordObject> onlyInSourcePasswordList = new List<PasswordObject> ();
            List<PasswordObject> onlyInDestinationPasswordList = new List<PasswordObject> ();

            ListSynchronizer.SynchronizeSortedList (sourcePasswordObjectList, destinationPasswordObjectList, comp,
                                                    onlyInSourcePasswordList, onlyInDestinationPasswordList);


            // check the number of item after synchronisation
            Assert.IsTrue (onlyInSourcePasswordList.Count == 2);
            Assert.IsTrue (onlyInDestinationPasswordList.Count == 1);

            // check if the corrects items are in the two list
            Assert.IsTrue (destinationPasswordObjectList[0].Id == inDestinationOnly1);
            Assert.IsTrue (onlyInSourcePasswordList[0].Id == inSourceOnly1 || onlyInSourcePasswordList[0].Id == inSourceOnly2);
            Assert.IsTrue (onlyInSourcePasswordList[1].Id == inSourceOnly1 || onlyInSourcePasswordList[1].Id == inSourceOnly2);

        } 
    }
}
